package com.xtedu.examproject.controller;

/*
 * Author: xzy
 * Date: 2025/10/13
 * Last Modified by: xzy
 * Last Modified date: 2025/10/13
 * Description: TODO - Add file description here
 */

import com.google.gson.Gson;
import com.xtedu.examproject.entity.QuestionType;
import com.xtedu.examproject.entity.Result;
import com.xtedu.examproject.service.QuestionTypeService;
import com.xtedu.examproject.util.CommonUtils;
import com.xtedu.examproject.util.PageHelper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/questionType/*")
public class QuestionTypeServlet extends HttpServlet {

    private QuestionTypeService questionTypeService;
    private Gson gson;

    @Override
    public void init() throws ServletException {
        questionTypeService = new QuestionTypeService();
        gson = new Gson();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();

        // 处理 null 或空路径
        if (pathInfo == null || pathInfo.equals("/") || pathInfo.isEmpty()) {
            findAll(req, resp);
            return;
        }

        // 标准化路径（去除前导和尾随斜杠）
        String path = pathInfo.replaceAll("^/|/$", "");

        switch (path) {
            case "findById" -> findById(req, resp);
            case "findAll" -> findAll(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }


    private void findAll(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<QuestionType> questionTypeList = questionTypeService.findAllQuestionType();
            if (questionTypeList == null) {
                CommonUtils.sendErrorResponse(resp, "获取题型数据失败");
                return;
            }
            CommonUtils.sendSuccessResponse(resp, "获取题型数据成功", questionTypeList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void findById(HttpServletRequest req, HttpServletResponse resp) {

    }

    @Override
    public void destroy() {
        super.destroy();
    }

    public QuestionTypeService getQuestionTypeService() {
        return questionTypeService;
    }

    public Gson getGson() {
        return gson;
    }
}
